package com.ruoyi.mng.service.impl;

import java.util.List;
import java.util.Random;
import java.util.function.Supplier;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mng.mapper.HzMerMerchantMapper;
import com.ruoyi.mng.domain.HzMerMerchant;
import com.ruoyi.mng.service.IHzMerMerchantService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商户信息Service业务层处理
 *
 * @author chenqiang
 * @date 2023-04-26
 */
@Service
public class HzMerMerchantServiceImpl implements IHzMerMerchantService {
    @Autowired
    private HzMerMerchantMapper hzMerMerchantMapper;

    /**
     * 查询商户信息
     *
     * @param merchantId 商户信息主键
     * @return 商户信息
     */
    @Override
    public HzMerMerchant selectHzMerMerchantByMerchantId(String merchantId) {
        return hzMerMerchantMapper.selectHzMerMerchantByMerchantId(merchantId);
    }

    /**
     * 查询商户信息列表
     *
     * @param hzMerMerchant 商户信息
     * @return 商户信息
     */
    @Override
    public List<HzMerMerchant> selectHzMerMerchantList(HzMerMerchant hzMerMerchant) {
        return hzMerMerchantMapper.selectHzMerMerchantList(hzMerMerchant);
    }

    /**
     * 新增商户信息
     *
     * @param hzMerMerchant 商户信息
     * @return 结果
     */
    @Override
    public int insertHzMerMerchant(HzMerMerchant hzMerMerchant) {
        if (hzMerMerchant.getMerchantSignType().equals(1)) {
            hzMerMerchant.setMerchantMd5Key(getRandomString(48));
        }
        hzMerMerchant.setMerchantId(getMerchantId());
        hzMerMerchant.setAddUserId(ShiroUtils.getUserId());
        hzMerMerchant.setDelFlag("0");
        hzMerMerchant.setCreateTime(DateUtils.getNowDate());
        hzMerMerchant.setUpdateTime(DateUtils.getNowDate());
        return hzMerMerchantMapper.insertHzMerMerchant(hzMerMerchant);
    }

    /**
     * description :: length用户要求产生字符串的长度
     */
    private static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /**
     * description :: 生成新商户号
     */
    private String getMerchantId() {
        String merchantId = hzMerMerchantMapper.selectMaxMerchantId();
        if (StringUtils.isEmpty(merchantId)) {
            //没有商户数据
            merchantId = "346109507680";
        }
        return String.valueOf(Long.parseLong(merchantId) + 7);
    }

    /**
     * 修改商户信息
     *
     * @param hzMerMerchant 商户信息
     * @return 结果
     */
    @Override
    public int updateHzMerMerchant(HzMerMerchant hzMerMerchant) {
        hzMerMerchant.setUpdateTime(DateUtils.getNowDate());
        return hzMerMerchantMapper.updateHzMerMerchant(hzMerMerchant);
    }

    /**
     * 批量删除商户信息
     *
     * @param merchantIds 需要删除的商户信息主键
     * @return 结果
     */
    @Override
    public int deleteHzMerMerchantByMerchantIds(String merchantIds) {
        return hzMerMerchantMapper.deleteHzMerMerchantByMerchantIds(Convert.toStrArray(merchantIds));
    }

    /**
     * 删除商户信息信息
     *
     * @param merchantId 商户信息主键
     * @return 结果
     */
    @Override
    public int deleteHzMerMerchantByMerchantId(String merchantId) {
        return hzMerMerchantMapper.deleteHzMerMerchantByMerchantId(merchantId);
    }
}
